#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pandas as pd
import requests
import time
import json
from datetime import datetime, timedelta

def newrankXhs(pull_date,type,token):
    if pull_date=="now":
        now = datetime.now() - timedelta(days=1)
        this_date = str(now.strftime('%Y-%m-%d'))
    else:
        this_date = pull_date

    headers = {
        'Content-Type': 'application/json',
        'n-token': token,
    }
    data_dict = {
        "start": 1,
        "size": 20,
        "rankType": 1,
        "rankRealType":1,
        "secondType":"",
        "type":[str(type)],
        "rankDate": this_date
    }
    data = json.dumps(data_dict, ensure_ascii=False)
    print(f"[{datetime.now()}] newrank 开始发送POST请求到: {data}")
    data =data.encode('utf-8')
    response = requests.post('https://gw.newrank.cn/api/mainRank/nr/mainRank/hotContent/getBiliHotContent', headers=headers, data=data)

    if response.status_code != 200:
        return "failure"
    else:
        print("http newrank is ok.....")
        json_data = response.json()

        original_list = json_data['data']['list']
        segments_data = {
            "segments": [
                {
                    "content": json.dumps(item, ensure_ascii=False),
                    "keywords": item.get('hotWords', [])
                }
                for item in original_list
            ]
        }
        return segments_data



def update_document_segments(pull_date,dataset_id, api_key,token):
    if pull_date=="now":
        now = datetime.now() - timedelta(days=1)
        this_date = str(now.strftime('%Y-%m-%d'))
    else:
        this_date = pull_date
    url = "http://localhost:8088/v1/datasets/"+dataset_id+"/document/create-by-text"
    headers = {
        'Authorization': f'Bearer {api_key}',
        'Content-Type': 'application/json'
    }
    categories = ["动画", "番剧", "全部", "连载动画", "完结动画", "资讯", "官方延伸", "音乐", "国创", "舞蹈", "游戏", "知识", "科技", "运动", "汽车", "生活", "美食", "动物圈", "鬼畜", "时尚", "娱乐", "影视", "纪录片", "电影", "电视剧"]
    payload = {
        "name": this_date + "号B站热门内容单日点赞榜",
        "text": "B站分类包含"+str(categories),
        "indexing_technique": "high_quality",
        "doc_form": "text_model",
        "process_rule": {"mode": "automatic"}
    }
    response = requests.post(url, headers=headers, data=json.dumps(payload))
    json_data = response.json()
    document_id =json_data['document']['id']
    print(f"document_id: {document_id}")

    for category in categories:
        text_data = newrankXhs(pull_date,category,token)
        url = "http://localhost:8088/v1/datasets/"+dataset_id+"/documents/"+document_id+"/segments"
        post_with_detailed_retry(url, headers, text_data)
    return "全部完成"


def post_with_detailed_retry(url, headers, text_data):
    """
    带详细日志的重试机制
    """
    max_retries = 3
    delay = 1

    print(f"[{datetime.now()}] 开始发送POST请求到: {url}")

    for attempt in range(1, max_retries + 1):
        try:
            print(f"[{datetime.now()}] 第{attempt}次尝试...")
            time.sleep(delay)
            responsesegments = requests.post(
                url,
                headers=headers,
                data=json.dumps(text_data)
            )

            print(f"[{datetime.now()}] 第{attempt}次尝试，状态码: {responsesegments.status_code}")

            # 如果不是404，成功返回
            if responsesegments.status_code != 404:
                print(f"[{datetime.now()}] 请求成功!")
                return "ok"

            # 如果是404且还有重试机会
            if attempt < max_retries:
                print(f"[{datetime.now()}] 状态码为404，{delay}秒后重试...")
            else:
                print(f"[{datetime.now()}] 已达到最大重试次数，最终状态码: {responsesegments.status_code}")

        except Exception as e:
            print(f"[{datetime.now()}] 第{attempt}次尝试出现异常: {e}")
            if attempt < max_retries:
                print(f"[{datetime.now()}] {delay}秒后重试...")
            else:
                print(f"[{datetime.now()}] 已达到最大重试次数，仍然失败")
                raise

    return "ok"


if __name__ == "__main__":
    dataset_id = '${dataset_id}'
    api_key = '${api_key}'
    pull_date = '${pull_date}'
    token = '${token}'
    print("开始 getWxHotContent")
    response_data = update_document_segments(pull_date,dataset_id, api_key,token)
    print("结束 getWxHotContent")